package com.site.service.impl.master;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.site.dao.master.PropertyDao;
import com.site.dto.master.PropertyBizBean;
import com.site.model.master.PropertyInfo;
import com.site.service.master.PropertyService;
import com.site.util.PropertyUtils;
import com.site.util.base.service.BaseServiceImpl;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

@Service("propertyService")
public class PropertyServiceImpl extends BaseServiceImpl implements
		PropertyService {

	@Autowired
	private PropertyDao propertyDao;

	@Override
	public Object doExecute(Object obj, String command) throws SystemException,
			ApplicationException {
		int result = 0;
		try {
			if (COMMAND_SAVE.equals(command)) {
				saveProperty((PropertyBizBean) obj);
			} else if (COMMAND_UPDATE.equals(command)) {
				updateProperty((PropertyBizBean) obj);
			} else if (COMMAND_DELETE.equals(command)) {
				propertyDao.markAsDeleted((Integer) obj);
			}
			result=1;
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	public List<PropertyBizBean> getAllProperty() {
		List<PropertyBizBean> propertyVoList = new ArrayList<PropertyBizBean>();
		List<PropertyInfo> propertyPoList = propertyDao.getAllProperty();
		if(null == propertyPoList || propertyPoList.isEmpty()){
			return null;
		}
		
		for(PropertyInfo po:propertyPoList) {
			PropertyBizBean vo = new PropertyBizBean();
			try {
				PropertyUtils.copyProperties(vo, po);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ApplicationException("从po到vo，属性拷贝错误！", e);
			}
			propertyVoList.add(vo);
		}
		return propertyVoList;
	}
	
	
	@Override
	public List<PropertyBizBean> getAllProperty(PropertyBizBean property, QueryPage queryPage) {
		List<PropertyInfo> poList =  propertyDao.getAllProperty(property, queryPage);
		List<PropertyBizBean> propertyVoList = new ArrayList<PropertyBizBean>();
		PropertyBizBean vo = null;
		for(PropertyInfo po:poList)
		{
			vo = new PropertyBizBean();
			try {
				PropertyUtils.copyProperties(vo, po);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ApplicationException("从po到vo，属性拷贝错误！", e);
			}
			propertyVoList.add(vo);
		}
		return propertyVoList;
	}
	
	@Override
	public PropertyBizBean getPropertyByKey(String propertyKey) {
		PropertyInfo po =  propertyDao.getPropertyKey(propertyKey);
		PropertyBizBean vo =new PropertyBizBean();
			try {
				PropertyUtils.copyProperties(vo, po);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ApplicationException("从po到vo，属性拷贝错误！", e);
			}
		return vo;
	}

	/**
	 * 增加属性
	 * @param vo
	 */
	public void saveProperty(PropertyBizBean vo)
	{
		PropertyInfo po = new PropertyInfo();
		try {
			PropertyUtils.copyProperties(po, vo);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		propertyDao.save(po);
	}
	
	/**
	 * 修改属性
	 * @param vo
	 */
	public void updateProperty(PropertyBizBean vo)
	{
		PropertyInfo po = (PropertyInfo)propertyDao.getPropertyById(vo.getPropertyId());
		try {
			PropertyUtils.copyNotNullProperties(po, vo);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		propertyDao.update(po);
	}
	
	/**
	 * 根据ID查找Property信息
	 * @param propertyId
	 * @return
	 */
	public PropertyBizBean getPropertyById(int propertyId)
	{
		PropertyInfo po = propertyDao.getPropertyById(propertyId);
		PropertyBizBean vo = new PropertyBizBean();
		try {
			PropertyUtils.copyProperties(vo, po);
		} catch (Exception e) {
			throw new ApplicationException(e);
		}
		return vo;
	}
	
	/**
	 * 根据Id逻辑删除Property
	 * @param codeId
	 * @return
	 */
	public int tombstoneProperty(int codeId){
		return propertyDao.tombstoneProperty(codeId);
	}
	
	@Override
	public List<String> findAllProperty() {
		return propertyDao.findAllProperty();
	}
	
	public boolean isExistsProertyKey(String propertyKey,int propertyId)
	{
		return propertyDao.isExistsProertyKey(propertyKey, propertyId);
	}

	public PropertyDao getPropertyDao() {
		return propertyDao;
	}

	public void setPropertyDao(PropertyDao propertyDao) {
		this.propertyDao = propertyDao;
	}
}
